//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace HappySprite
{
    public class InputManager : GameComponent
    {
        class KeyboardBinding
        {
            public Keys Key;
            public MemberInfo MemberInfo;
            public object Target;
            public bool Pressed;
        }

        Dictionary<Keys, KeyboardBinding> keyboardBindings = new Dictionary<Keys, KeyboardBinding>();

        public InputManager(Game game)
            : base(game)
        {
            game.Components.Add(this);
        }

        // binds to a boolean field or property or method that takes a boolean on an object
        public void Add(Keys key, object target, string member)
        {
            // Check arguments
            if (target == null) throw new ArgumentNullException("target");
            if (string.IsNullOrEmpty(member)) throw new ArgumentException("member");

            MemberInfo memberInfo = MemberHelper.GetMember(target, member);

            KeyboardBinding binding = new KeyboardBinding();

            binding.Key = key;
            binding.MemberInfo = memberInfo;
            binding.Target = target;

            keyboardBindings[key] = binding;
        }

        public void Remove(Keys key)
        {
            if (keyboardBindings.ContainsKey(key))
                keyboardBindings.Remove(key);
        }

        public override void Update(GameTime gameTime)
        {
            // Nothing to do
            if (keyboardBindings.Count == 0)
                return;

            // Get the current keyboard state
            KeyboardState keyboardState = Keyboard.GetState();

            foreach (KeyboardBinding binding in keyboardBindings.Values)
            {
                // Get current key state
                bool pressed = (keyboardState.IsKeyDown(binding.Key));

                // State change from last frame
                if (binding.Pressed != pressed)
                {
                    binding.Pressed = pressed;
                    MemberHelper.InvokeMember(binding.Target, binding.MemberInfo, pressed);
                }
            }
        }
    }
}
